﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSProduct
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblProduct> getAll()
        {
            try
            {
                var list = from a in objDB.tblProducts
                           select a;
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// lấy các sản phẩm theo tiêu chí status , factory id, cate id
        /// </summary>
        /// <param name="factoryid">ID hãng sản xuất</param>
        /// <param name="cateid">ID Loại sản phẩm</param>
        /// <param name="status">Trạng thái sản phẩm</param>
        /// <returns>Danh sách sản phẩm </returns>
        public List<tblProduct> getAll(int factoryid,int cateid,bool status)
        {
            try
            {
                var list = from a in objDB.tblProducts
                           where a.tblCate_Factory.FactoryID.Equals(factoryid) && 
                           a.tblCate_Factory.CateID.Equals(cateid) &&
                           a.Status.Equals(status)
                           select a;
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Lấy danh sách theo status
        /// </summary>
        /// <param name="status"> Trạng thái 0 là đã xóa</param>
        /// <returns>danh sách </returns>
        public List<tblProduct> getStatus(bool status)
        {
            try
            {
                var list = from a in objDB.tblProducts
                           where a.Status.Equals(status)
                           select a;
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// Lấy danh sách các sản phẩm hót
        /// </summary>
        /// <param name="hot"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<tblProduct> getTopProductHot(bool hot,int top)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                           where a.Hot.Equals(hot) && a.Visible==true &&a.Status==true
                           select a).Take(top);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductNew(bool news, int top)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.News.Equals(news) && a.Visible == true && a.Status == true
                            select a).Take(top);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductHot(bool hot)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.Hot.Equals(hot) && a.Visible == true && a.Status == true
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getTopProductNew(bool news)
        {
            try
            {
                var list = (from a in objDB.tblProducts
                            where a.News.Equals(news) && a.Visible == true && a.Status == true
                            select a);
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblProduct> getVisible(bool visible)
        {
            try
            {
                var list = from a in objDB.tblProducts
                           where a.Visible.Equals(visible)
                           select a;
                return list.ToList<tblProduct>();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public tblProduct getById(int id)
        {
            try
            {
                var obj = objDB.tblProducts.SingleOrDefault(a => a.ID == id);
                return obj;
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblProduct getBySerial(string serial)
        {
            try
            {
                var obj = objDB.tblProducts.SingleOrDefault(a => a.SeriNumber == serial);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void insert(tblProduct obj)
        {
            try
            {
                objDB.tblProducts.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblProduct obj)
        {
            try
            {
                var objj = objDB.tblProducts.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.Detail = obj.Detail;
                    objj.ID = obj.ID;
                    objj.Image = obj.Image;
                    objj.Name = obj.Name;
                    objj.Number = obj.Number;
                    objj.Status = obj.Status;
                    objj.Visible = obj.Visible;
                    objj.Price = obj.Price;
                    objj.FactoryID = obj.FactoryID;
                    objj.Summary = obj.Summary;
                    objj.SeriNumber = obj.SeriNumber;
                    objj.View = obj.View;
                    objj.Sale = obj.Sale;
                    objj.Hot = obj.Hot;
                    objj.News = obj.News;
                    objj.Status = true;
                    objj.Visible = true;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void update(int id, bool status)
        {
            try
            {
                var obj = objDB.tblProducts.SingleOrDefault(a => a.ID == id);
                if (obj != null)
                {
                    obj.Status = status;
                    obj.Visible = true;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblProducts
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblProducts.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
